package servlet;

import bean.Repertory;
import com.github.pagehelper.PageInfo;
import dao.impl.RepertoryDaoImpl;
import dao.impl.UserDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/RepertoryServlet")
public class RepertoryServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");

        String method=req.getParameter("method");

        if (method.equals("ReperAllFind")){
            List<Repertory> list=new RepertoryDaoImpl().ReperAllFind();
            System.out.println(list);
            req.setAttribute("repertory",list);
            req.getRequestDispatcher("repertory.jsp").forward(req,resp);

        }else if (method.equals("SeachNameReper")){
            String warehousename=req.getParameter("warehousename");
            if(warehousename.equals("Search")){
                resp.sendRedirect("RepertoryServlet?method=ReperAllFind");
            }else{
                List<Repertory> list=new RepertoryDaoImpl().nameSerch(warehousename);

                req.setAttribute("repertory",list);
                req.getRequestDispatcher("repertory.jsp").forward(req,resp);
            }
        }else if (method.equals("SeachidReper")){
            int id=Integer.parseInt(req.getParameter("warehouseId"));
            List<Repertory> list=new RepertoryDaoImpl().idSerch(id);
            req.setAttribute("repertory",list);
            req.getRequestDispatcher("repertory.jsp").forward(req,resp);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
